package com.zhiyou.crowdfunding.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiyou.crowdfunding.bean.Projects;
import com.zhiyou.crowdfunding.common.Meta;
import com.zhiyou.crowdfunding.common.ResultObject;
import com.zhiyou.crowdfunding.service.ProjectsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jyf
 * @since 2022-05-27
 */
@RestController
@Slf4j
@RequestMapping("/projects")
public class ProjectsController {

    @Autowired
    private ProjectsService projectsService;

    /**
     * 查询所有项目，并且分页
     * @param pageNum  页数
     * @param pageSize  每页显示条数
     * @return
     */
    @GetMapping("/getAllProjects/{pageNum}/{pageSize}")
    public ResultObject getAllProjects(@PathVariable("pageNum") Integer pageNum,
                                       @PathVariable("pageSize") Integer pageSize){
        ResultObject resultObject = new ResultObject();
        Meta meta = new Meta();
        if (pageNum==null || pageSize==null){
            meta.setMsg("页码或者条数为空");
            meta.setStatus(400);
            resultObject.setData(meta);
            return resultObject;
        }
        resultObject.setData(projectsService.getAllProjects(pageNum,pageSize));
        meta.setStatus(200);
        meta.setMsg("请求成功");
        resultObject.setMeta(meta);
        return resultObject;
    }


    /**
     * 根据条件查询项目
     * @param psPstId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getProjectsByCondition/{pageNum}/{pageSize}")
    public ResultObject getProjectsByPst(@RequestParam(value = "psPstId",required = false) Integer psPstId,
                                           @RequestParam(value = "usName",required = false) String usName,
                                           @RequestParam(value = "psName",required = false) String psName,
                                           @RequestParam(value = "psType",required = false) Integer psType,
                                           @PathVariable("pageNum") Integer pageNum,
                                           @PathVariable("pageSize") Integer pageSize){
        ResultObject r = new ResultObject();
        Meta meta = new Meta();

        Page<Projects> projectsByPst = projectsService.getProjectsByCondition(psPstId,psName,usName,psType, pageNum, pageSize);
        if (projectsByPst==null){
            meta.setMsg("查无此项目");
            meta.setStatus(400);
            r.setMeta(meta);
            return r;
        }
        meta.setStatus(200);
        meta.setMsg("查询成功");
        r.setMeta(meta);
        r.setData(projectsByPst);
        return r;

    }




}

